package com.pda.gongpin.ui.abnormalcheck;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.pda.gongpin.R;
import com.pda.gongpin.bean.AbnormalDataByContainerBean;
import com.pda.gongpin.bean.DropDownByEntryNoBean;
import com.pda.gongpin.bean.ScanBean;
import com.pda.gongpin.bean.ScanResultBean;
import com.pda.gongpin.bean.request.QRcodeRequest;
import com.pda.gongpin.contract.AbnormalCheckContract;
import com.pda.gongpin.dialog.AffirmDialog;
import com.pda.gongpin.event.NoEventBean;
import com.pda.gongpin.net.NetUtils;
import com.pda.gongpin.presenter.AbnormalCheckPresenter;
import com.pda.gongpin.utils.PopUtisl;
import com.pda.gongpin.view.ScanButView;
import com.wangshen.base.net.bean.BaseAppEntity;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpFragment;
import com.wangshen.base.util.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class AbnormalCheckFragment extends BaseMvpFragment<AbnormalCheckPresenter> implements AbnormalCheckContract.View {
    @BindView(R.id.tv_deliveryNo)
    TextView tvDeliveryNo;
    @BindView(R.id.tv_pallet)
    TextView tvPallet;
    @BindView(R.id.tv_sku)
    TextView tvSku;
    @BindView(R.id.tv_materialCode)
    TextView tvMaterialCode;
    @BindView(R.id.tv_articleId)
    TextView tvArticleId;
    @BindView(R.id.tv_articleName)
    TextView tvArticleName;
    @BindView(R.id.tv_spec)
    TextView tvSpec;
    @BindView(R.id.tv_brand)
    TextView tvBrand;
    @BindView(R.id.tv_packageSpecFull)
    TextView tvPackageSpecFull;
    @BindView(R.id.tv_surfaceTreatment)
    TextView tvSurfaceTreatment;
    @BindView(R.id.et_boxNmu)
    EditText etBoxNmu;
    @BindView(R.id.et_piecesNum)
    EditText etPiecesNum;
    @BindView(R.id.sb_scan)
    ScanButView sbScan;
    @BindView(R.id.tv_recommend)
    TextView tvRecommend;
    @BindView(R.id.tv_back_storage)
    TextView tvBackStorage;
    String entryNo;
    @BindView(R.id.ll_Num)
    LinearLayout llNum;
    private String id;
    private String newEntryNumberId;

    @Override
    public AbnormalCheckPresenter getPresenter() {
        return new AbnormalCheckPresenter();
    }

    @Override
    public int getViewId() {
        return R.layout.fragment_abnormal_check;
    }

    @Override
    public void initView() {
        EventBus.getDefault().register(this);
    }
    @Subscribe
    public void onEvent(NoEventBean eventBean){
        tvDeliveryNo.setText("入库单信息: " + eventBean.getNo());
    }
    @Override
    public void initLinsenterner() {
        sbScan.setScanCallBack(new ScanButView.ScanCallBack() {
            @Override
            public void callBack(ScanBean scanBean) {
                QRcodeRequest request = new QRcodeRequest();
                request.setQRCode(scanBean.getCode());
                getCode(request);
            }
        });
    }

    public void getCode(QRcodeRequest request) {
        NetUtils.geApi().scanTheQRCodeToVerify(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseAppEntity<ScanResultBean>>() {
                    @Override
                    public void accept(BaseAppEntity<ScanResultBean> stringBaseAppEntity) throws Exception {
                        if (!stringBaseAppEntity.getCode().equals("200")) {
                            ToastUtils.showToast(getContext(), stringBaseAppEntity.getMessage());
                            return;
                        }
                        if (stringBaseAppEntity.getData().getType().equals("A")) {
                            tvPallet.setText(stringBaseAppEntity.getData().getCode());
                            presenter.getAbnormalDataByContainer(stringBaseAppEntity.getData().getCode());
                        } else {
                            ToastUtils.showToast(getContext(), "请扫正确的条形码");
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        ToastUtils.showToast(getContext(), throwable.getMessage());
                    }
                });
    }

    @Override
    public void getData() {

    }

    @OnClick({R.id.tv_sku, R.id.tv_recommend, R.id.tv_back_storage})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_sku:
                if (TextUtils.isEmpty(entryNo)) {
                    showMessage("请先扫容器号");
                    return;
                }
                presenter.getArticleIdDropDownByEntryNo(entryNo);
                break;
            case R.id.tv_recommend:
                if (TextUtils.isEmpty(etBoxNmu.getText())) {
                    showMessage("箱数不能为空！");
                    return;
                }
                String piecesNum = "0";
                if (!TextUtils.isEmpty(etPiecesNum.getText())) {
                    piecesNum = etPiecesNum.getText().toString();
                }
                String finalPiecesNum = piecesNum;
                AffirmDialog dialog = new AffirmDialog(mContext, new AffirmDialog.OnConfrim() {
                    @Override
                    public void onConfrim() {
                        presenter.abnormalConfirm(id, newEntryNumberId, etBoxNmu.getText().toString(), finalPiecesNum);
                    }
                });
                dialog.show("确认要盘点吗？");
                break;
            case R.id.tv_back_storage:

                break;
        }
    }

    @Override
    public void setData(AbnormalDataByContainerBean containerBean) {
        if (containerBean.getStatus().equals("10")) {
            tvRecommend.setVisibility(View.VISIBLE);
            tvBackStorage.setVisibility(View.GONE);
            llNum.setVisibility(View.VISIBLE);
        } else if (containerBean.getStatus().equals("20")) {
            tvRecommend.setVisibility(View.GONE);
            tvBackStorage.setVisibility(View.VISIBLE);
            llNum.setVisibility(View.GONE);
        } else {
            tvRecommend.setVisibility(View.GONE);
            tvBackStorage.setVisibility(View.GONE);
            llNum.setVisibility(View.GONE);
        }
        id = containerBean.getId();
        newEntryNumberId = containerBean.getEntryNumberId();
        entryNo = containerBean.getEntryNo();
        tvSpec.setText("规格：" + containerBean.getSpec());
        tvArticleId.setText("物品编码： " + containerBean.getArticleId());
        tvArticleName.setText("物品名称：" + containerBean.getArticleName());
        tvSurfaceTreatment.setText("表色： " + containerBean.getSurfaceTreatment());
        tvBrand.setText("品牌：" + containerBean.getBrand());
        tvPackageSpecFull.setText("包格：" + containerBean.getPackageSpecFull());
        tvMaterialCode.setText("我的料号" + containerBean.getMaterialCode());
    }

    @Override
    public void setSelectData(List<DropDownByEntryNoBean> list) {
        PopUtisl.showSelectPop(mContext, list, newEntryNumberId, tvSku, new PopUtisl.SelectRawClick() {
            @Override
            public void onClick(DropDownByEntryNoBean containerBean) {
                tvSku.setText(containerBean.getShowStr());
                newEntryNumberId = containerBean.getEntryNumberId();
                tvSpec.setText("规格：" + containerBean.getSpec());
                tvArticleId.setText("物品编码： " + containerBean.getArticleId());
                tvArticleName.setText("物品名称：" + containerBean.getArticleName());
                tvSurfaceTreatment.setText("表色： " + containerBean.getSurfaceTreatment());
                tvBrand.setText("品牌：" + containerBean.getBrand());
                tvPackageSpecFull.setText("包格：" + containerBean.getPackageSpecFull());
                tvMaterialCode.setText("我的料号" + containerBean.getMaterialCode());
            }
        }, "请选择物料！");
    }

    @Override
    public void onSuccess() {
        tvRecommend.setVisibility(View.GONE);
        tvBackStorage.setVisibility(View.GONE);
        llNum.setVisibility(View.GONE);
        tvPallet.setText("");
        tvSku.setText("");
        id="";
        entryNo="";
        newEntryNumberId="";
        tvSpec.setText("规格：" );
        tvArticleId.setText("物品编码： " );
        tvArticleName.setText("物品名称：" );
        tvSurfaceTreatment.setText("表色： " );
        tvBrand.setText("品牌：" );
        tvPackageSpecFull.setText("包格：" );
        tvMaterialCode.setText("我的料号" );
    }
}
